/**
 * 
 */
package org.ediba.siri.web.rest;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.ediba.siri.core.model.domain.Model;
import org.ediba.siri.core.model.domain.ModelEmail;
import org.ediba.siri.core.model.service.ModelService;
import org.json.JSONException;
import org.springframework.web.bind.annotation.ResponseBody;

public class ModelEmailController extends Controller {

	private ModelService modelService;
	public void setModelService(ModelService modelService) {this.modelService = modelService;}
	
	@ResponseBody
	public Map<String,Object> postHandler(HttpServletRequest request, HttpServletResponse response, ModelEmailCommand command){

		Integer idModel = null;
		String email = null;
		
		idModel = command.getIdModel();

		try {email = command.getData().getString("email");
		} catch (JSONException e) {e.printStackTrace();}
		
		ModelEmail modelEmail = this.modelService.emailCreate(email,idModel);
		
		Map<String,Object> _result = new HashMap<String,Object>(2);
		_result.put("data", modelEmail);
		_result.put("success", true);
		return _result;
	}
	
	@ResponseBody
	public Map<String,Object> getHandler(HttpServletRequest request, HttpServletResponse response, ModelEmailCommand command){
		List<ModelEmail> modelEmails = null;
		
		Integer idModel = null;
		
		idModel = command.getIdModel();
		
		if (idModel!=null){
			Model model = this.modelService.retrieve(idModel);
			modelEmails = new ArrayList<ModelEmail>(model.getModelemails());
		}else{
			modelEmails = this.modelService.emailRetrieve();
		}
		Map<String,Object> _result = new HashMap<String,Object>(4);
		_result.put("data", modelEmails);
		_result.put("total", modelEmails.size());
		_result.put("success", true);
		_result.put("message", "OK");
		return _result;
	}
	
	@ResponseBody
	public Map<String,Object> putHandler(HttpServletRequest request, HttpServletResponse response, ModelEmailCommand command){
		
		Integer id = null;
		String email = null;
		
		try {id = command.getData().getInt("id");
		} catch (JSONException e) {e.printStackTrace();}
		try {email = command.getData().getString("email");
		} catch (JSONException e) {e.printStackTrace();}
		
		ModelEmail modelEmail = this.modelService.emailRetrieve(id);
		if (email!=null)
			modelEmail.setEmail(email);
		
		modelEmail = this.modelService.emailUpdate(modelEmail);
		
		Map<String,Object> _result = new HashMap<String,Object>(2);
		_result.put("success", true);
		_result.put("data", modelEmail);
		return _result;
	}
	
	@ResponseBody
	public Map<String,Object> deleteHandler(HttpServletRequest request, HttpServletResponse response, ModelEmailCommand command){

		Integer id = null;
		try {id = command.getData().getInt("id");}
		catch (JSONException e) {e.printStackTrace();}
		
		boolean _success = this.modelService.emailDelete(id);
		
		Map<String,Object> _result = new HashMap<String,Object>(1);
		_result.put("success", _success);
		return _result;
	}
}
